{**
 * Prescription list
 *
 * @package   OpenEMR
 * @link      http://www.open-emr.org
 * @author    Brady Miller <brady.g.miller@gmail.com>
 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
 * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
 *}
<html>
<head>

{headerShow}

<link rel="stylesheet" href="{$GLOBALS.css_header}" type="text/css">
<script type="text/javascript" src="{$GLOBALS.assets_static_relative}/jquery-min-1-2-2/index.js"></script>

{literal}
<style type="text/css" title="mystyles" media="all">
.inactive {
  color:#777777;
}
</style>

<script language="javascript">

function changeLinkHref(id,addValue,value) {
    var myRegExp = new RegExp(":" + value + ":");
    if (addValue){ //add value to href
        document.getElementById(id).href += ':' + value + ':';
    }
    else { //remove value from href
       document.getElementById(id).href = document.getElementById(id).href.replace(myRegExp,'');
    }
}

function changeLinkHrefAll(addValue, value) {
    changeLinkHref('multiprint', addValue, value);
    changeLinkHref('multiprintcss', addValue, value);
    changeLinkHref('multiprintToFax', addValue, value);
}

//uses jquery to update database table
function markTx(oThis,id){
    var isChecked = oThis.checked ? 1 : 0; // @TODO this is better served if checkboxs are all checked on submit with one call to markTx
    $.get('./interface/weno/markTx.php?rx='+id+'&state='+isChecked);
    return false;
}

function changeLinkHref_All(id,addValue,value) {
    var myRegExp = new RegExp(":" + value + ":");
    if (addValue){ //add value to href
        document.getElementById(id).href += ':' + value + ':';
    }
    else { //remove value from href
        document.getElementById(id).href = document.getElementById(id).href.replace(myRegExp,'');
        // TajEmo Work By CB 2012/06/14 02:17:16 PM remove the target change
    //document.getElementById(id).target = '';
    }
}

function Check(chk) {
    var len=chk.length;
    if (len==undefined) {chk.checked=true;}
    else {
        for (pr = 0; pr < chk.length; pr++){
            if($(chk[pr]).parents("tr.inactive").length==0)
                {
                    chk[pr].checked=true;
                    changeLinkHref_All('multiprint',true,chk[pr].value);
                    changeLinkHref_All('multiprintcss',true, chk[pr].value);
                    changeLinkHref_All('multiprintToFax',true, chk[pr].value);
                }
        }
    }
}

function Uncheck(chk) {
    var len=chk.length;
    if (len==undefined) {chk.checked=false;}
    else {
        for (pr = 0; pr < chk.length; pr++){
            chk[pr].checked=false;
            changeLinkHref_All('multiprint',false,chk[pr].value);
            changeLinkHref_All('multiprintcss',false, chk[pr].value);
            changeLinkHref_All('multiprintToFax',false, chk[pr].value);
        }
    }
}

var CheckForChecks = function(chk) {
    // Checks for any checked boxes, if none are found than an alert is raised and the link is killed
    if (Checking(chk) == false) { return false; }
    return top.restoreSession();
};

function Checking(chk) {
    var len=chk.length;
    var foundone=false;

    if (len==undefined) {
            if (chk.checked == true){
                foundone=true;
            }
    }
    else {
        for (pr = 0; pr < chk.length; pr++){
            if (chk[pr].checked == true) {
                foundone=true;
            }
        }
    }
    if (foundone) {
        return true;
    } else {
        alert("{/literal}{xl t='Please select at least one prescription!'}{literal}");
        return false;
    }
}

$(document).ready(function(){
  $(":checkbox:checked").each(function () {
      changeLinkHref('multiprint',this.checked, this.value);
      changeLinkHref('multiprintcss',this.checked, this.value);
      changeLinkHref('multiprintToFax',this.checked, this.value);
  });
})

</script>

{/literal}
</head>
<body class="body_top">

{if $prescriptions}
<span class="title"><b>{xl t='List'|escape:'html'}</b></span>

<div id="prescription_list">

<form name="presc">

<div id="print_links">
    <table width="100%">
        <tr>
            <td align="left">
                <table>
                    <tr>
                        <td>
                            <a id="multiprint" href="{$CONTROLLER}prescription&multiprint&id={$printm}" onclick="top.restoreSession()" class="css_button"><span>{xl t='Download'|escape:'html'} ({xl t='PDF'|escape:'html'})</span></a>
                        </td>
                        <td>
                          <!-- TajEmo work by CB 2012/06/14 02:16:32 PM target="_script" opens better -->
                            <a target="_script" id="multiprintcss" href="{$CONTROLLER}prescription&multiprintcss&id={$printm}" onclick="top.restoreSession()" class="css_button"><span>{xl t='View Printable Version'|escape:'html'} ({xl t='HTML'|escape:'html'})</span></a>
                        </td>
                        <td style="border-style:none;">
                            <a id="multiprintToFax" href="{$CONTROLLER}prescription&multiprintfax&id={$printm}" onclick="top.restoreSession()" class="css_button"><span>{xl t='Download'|escape:'html'} ({xl t='Fax'|escape:'html'})</span></a>
                        </td>

                        {if $GLOBALS.weno_rx_enable}
                            <td style="border-style:none;">
                                <a id="multiprintToFax" href="{$GLOBALS.webroot}/interface/weno/validate.php" onclick="top.restoreSession()" class="css_button"><span>{xl t='Transmit'|escape:'html'} {xl t='Rx'|escape:'html'}</span></a>
                            </td>
                        {/if}
                        {if $CAMOS_FORM == true}
                            <td>
                                <a id="four_panel_rx" href="{$GLOBALS.webroot}/interface/forms/CAMOS/rx_print.php?sigline=plain" onclick="top.restoreSession()" class="css_button"><span>{xl t='View Four Panel'|escape:'html'}</span></a>
                            </td>
                        {/if}
                    </tr>
                </table>
            </td>
            <td align="right">
                <table>
                <tr>
                    <td>
                        <a href="#" class="small" onClick="Check(document.presc.check_list);"><span>{xl t='Check All'|escape:'html'}</span></a> |
                        <a href="#" class="small" onClick="Uncheck(document.presc.check_list);"><span>{xl t='Clear All'|escape:'html'}</span></a>
                    </td>
                </tr>
                </table>
            </td>
        </tr>
    </table>
</div>


<table width="100%" class="showborder_head" cellspacing="0px" cellpadding="2px">
    <tr>
       <!-- TajEmo Changes 2012/06/14 02:01:43 PM by CB added Heading for checkbox column -->
        <th width="8px">&nbsp;</th>
            <th width="8px">&nbsp;</th>

        <th width="70px">{xl t='Drug'|escape:'html'}</th>
        <th width="70px">{xl t='RxNorm'|escape:'html'}</th>
        <th width="70px">{xl t='Created'|escape:'html'}<br />{xl t='Changed'|escape:'html'}</th>
        <th width="20px">{xl t='Dosage'|escape:'html'}</th>
        <th width="20px">{xl t='Qty'|escape:'html'}.</th>
        <th>{xl t='Unit'|escape:'html'}</th>
        <th width="20px">{xl t='Refills'|escape:'html'}</th>
        <th>{xl t='Provider'|escape:'html'}</th>
        {if $GLOBALS.weno_rx_enable}
            <th>{xl t='Send'|escape:'html'}<br>{xl t='NewRx'|escape:'html'}</th>
            <th>{xl t='Refill'|escape:'html'}<br>{xl t='Rx'|escape:'html'}</th>
            <th>{xl t='Last'|escape:'html'}<br>{xl t='Tx Date'|escape:'html'}</th>
        {/if}
    </tr>

    {foreach from=$prescriptions item=prescription}
  <!-- TajEmo Changes 2012/06/14 02:03:17 PM by CB added cursor:pointer for easier user understanding -->
  <tr style="cursor:pointer" id="{$prescription->id|escape:'html'}" class="showborder onescript {if $prescription->active <= 0} inactive{/if}" title="{xl t='Click to view/edit'|escape:'html'}">
     <td align="center">
      <input class="check_list" id="check_list" type="checkbox" value="{$prescription->id|escape:'html'}" {if $prescription->encounter == $prescription->get_encounter() && $prescription->active > 0}checked="checked" {/if}onclick="changeLinkHref('multiprint',this.checked, this.value);changeLinkHref('multiprintcss',this.checked, this.value);changeLinkHref('multiprintToFax',this.checked, this.value)" title="{xl t='Select for printing'|escape:'html'}">
    </td>
    {if $prescription->erx_source==0}
    <td class="editscript"  id="{$prescription->id}">
        <a class='editscript css_button_small' id='{$prescription->id}' href="controller.php?prescription&edit&id={$prescription->id}" style="margin-top:-2px"><span>{xl t='Edit'|escape:'html'}</span></a>
      <!-- TajEmo Changes 2012/06/14 02:02:22 PM by CB commented out, to avoid duplicate display of drug name
        {if $prescription->active > 0}<b>{/if}{$prescription->drug}{if $prescription->active > 0}</b>{/if}&nbsp;
      -->
    </td>
    <td class="editscript"  id="{$prescription->id}">
    {if $prescription->active > 0}<b>{/if}{$prescription->drug|escape:'html'}{if $prescription->active > 0}</b>{/if}&nbsp;
  <br />{$prescription->note|escape:'html'}
    </td>
    {else}
  <td>&nbsp;</td>
    <td id="{$prescription->id}">
    {if $prescription->active > 0}<b>{/if}{$prescription->drug|escape:'html'}{if $prescription->active > 0}</b>{/if}&nbsp;
  <br />{$prescription->note|escape:'html'}
    </td>
    {/if}
    <td id="{$prescription->id}">
      {$prescription->rxnorm_drugcode|escape:'html'}&nbsp;
    </td>
    <td id="{$prescription->id}">
      {$prescription->date_added|escape:'html'}<br />
      {$prescription->date_modified|escape:'html'}&nbsp;
    </td>
    <td id="{$prescription->id}">
      {$prescription->get_dosage_display()|escape:'html'} &nbsp;
    </td>
    {if $prescription->erx_source==0}
    <td class="editscript" id="{$prescription->id}">
      {$prescription->quantity|escape:'html'} &nbsp;
    </td>
    {else}
    <td id="{$prescription->id}">
      {$prescription->quantity|escape:'html'} &nbsp;
    </td>
    {/if}
    <td id="{$prescription->id}">
       {$prescription->get_size()|escape:'html'} {$prescription->get_unit_display()|escape:'html'}&nbsp;
    </td>
    <td id="{$prescription->id}">
      {$prescription->refills|escape:'html'} &nbsp;
    </td>
    <td id="{$prescription->id}">
      {$prescription->provider->get_name_display()|escape:'html'}&nbsp;
    </td>
    {if $GLOBALS.weno_rx_enable}
        <td align="center">
        {if $prescription->ntx == 0}
            <input class="check_list" id="NewRx-{$smarty.foreach.rx.index|escape:'html'}" type="checkbox" {if $prescription->active < 1}disabled="disabled" {/if} value="{$prescription->id|escape:'html'}" {if $prescription->ntx > 0 && $prescription->active > 0}checked="checked" {/if} onClick="markTx(this,'NewRx-{$prescription->id|escape:'html'}'); changeLinkHrefAll(this.checked, this.value);" title={xl t="Select to transmit"|escape:'html'}>
        {/if}
        </td>
        <td align="center">
        {if $prescription->ntx == 1}
            <input class="check_list" id="RefillRx-{$smarty.foreach.rx.index|escape:'html'}" type="checkbox" {if $prescription->active < 1}disabled="disabled" {/if} value="{$prescription->id|escape:'html'}" {if $prescription->ntx > 0 && $prescription->active > 0}checked="checked" {/if} onClick="markTx(this,'RefillRx-{$prescription->id|escape:'html'}'); changeLinkHrefAll(this.checked, this.value);" title={xl t="Select to transmit"|escape:'html'}>
        {/if}
        </td>
        <td id="{$prescription->id|escape:'html'}" align="center">
        {$prescription->txDate|escape:'html'}
        </td>
    {/if}
  </tr>
    {/foreach}

</table>

</form>
</div>

{else}
<div class="text" style="margin-top:10px">{xl t='There are currently no prescriptions'|escape:'html'}.</div>
{/if}

</body>
{literal}
<script language='JavaScript'>

$(document).ready(function(){
$("#multiprint").click(function() { return CheckForChecks(document.presc.check_list); });
$("#multiprintcss").click(function() { return CheckForChecks(document.presc.check_list); });
$("#multiprintToFax").click(function() { return CheckForChecks(document.presc.check_list); });
$(".editscript").click(function() { ShowScript(this); });
$(".onescript").mouseover(function() { $(this).children().toggleClass("highlight"); });
$(".onescript").mouseout(function() { $(this).children().toggleClass("highlight"); });
});

var ShowScript = function(eObj) {
    top.restoreSession();
    objID = eObj.id;
    document.location.href="{/literal}{$GLOBALS.webroot}{literal}/controller.php?prescription&edit&id="+objID;
    return true;
};

</script>
{/literal}
</html>
